<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02 - 每个顶点显示一种颜色</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
    <!-- 引入三维引擎 -->
    <script src="../external/three-js-r93/build/three.js"></script>
    <!-- 扩展控件 OrbitControls -->
    <script src="../external/three-js-r93/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>

    let scene = new THREE.Scene();

    // buffer 类型几何体对象
    let geometry = new THREE.BufferGeometry();
    console.log(geometry);

    // 类型数组创建顶点数据
    let vertices = new Float32Array([
        0, 0, 0,   /* 顶点1 */
        50, 0, 0,  /* 顶点2 */
        0, 100, 0, /* 顶点3 */
        0, 0, 10,  /* 顶点4 */
        0, 0, 100, /* 顶点5 */
        50, 0, 0   /* 顶点6 */
    ]);

    /* 创建属性缓冲区对象 */
    let attribute = new THREE.BufferAttribute(vertices, 3);/* 3个为一组表示一个顶点坐标 xyz */

    /* 这是几何体 attributes 的位置属性 */
    geometry.attributes.position = attribute;

    // 类型化数组创建顶点颜色
    let colros = new Float32Array([
        1, 0, 0, // 顶点的颜色
        0, 1, 0,
        0, 0, 1,
        0.5, 0, 0,
        0, 0.5, 0,
        0, 0, 0.5
    ]);

    // 这只几何体 attributes 属性的颜色 color属性
    geometry.attributes.color = new THREE.BufferAttribute(colros, 3);

    /* 网格渲染模式 */
    // let material = new THREE.MeshBasicMaterial({
    //     color:0xEE7942,
    //     side:THREE.DoubleSide
    // });
    // let mesh = new THREE.Mesh(geometry, material);
    // scene.add(mesh);

    /* 点渲染模式 */
    let material = new THREE.PointsMaterial({
        vertexColors:THREE.VertexColors, // 通过顶点将颜色差值进行计算
        size:5.0//点对象像素尺寸
    });
    let points = new THREE.Points(geometry, material);
    scene.add(points);

    /* 线条渲染模式 */
    // let material = new THREE.LineBasicMaterial({
    //     color:0xff0000
    // });
    // let line = new THREE.Line(geometry, material);
    // scene.add(line);

    /* 添加辅助坐标 */
    let axesHelper = new THREE.AxesHelper(200);
    scene.add(axesHelper);

    /* 添加相机 */
    let width = window.innerWidth;
    let height = window.innerHeight;
    let k = width / height; // 窗口宽高比
    let s = 150; // 三维场景显示范围控制系数， 系数越大，显示的范围越大

    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200);
    camera.lookAt(scene.position);

    /* 渲染器 */
    let renderer = new THREE.WebGLRenderer({antialis: true});// 抗锯齿
    renderer.setSize(width, height);//渲染区域尺寸
    renderer.setClearColor(0xE0EEE0, 1);//渲染背景色

    document.body.appendChild(renderer.domElement); // body 元素中插入 canvas 对象

    /*渲染函数*/
    function render() {

        renderer.render(scene, camera);

    }

    render();

    /* 添加控件对象 */
    let controls = new THREE.OrbitControls(camera, renderer.domElement);// 控件可以监听鼠标的变化，改变相机的属性
    controls.addEventListener('change', render);//添加鼠标监听，触发渲染函数，更新 canvas 画布渲染效果

</script>

</body>
</html>